package action.resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import model.Link;
import model.Page;
import model.ResourceFile;
import model.Resource;
import model.ResourceMessage;
import model.User;

import DAO.ResourceDAO;
import DAO.DAOImple.ResourceImple;

import com.opensymphony.xwork2.ActionSupport;

public class ResourceAction extends ActionSupport {
	private Resource resource;
	private User user;
	private ResourceFile resourceFile;
	private List<Resource> resources;
	private ResourceMessage resourceMessage;
	private List<ResourceMessage> resourceMessages;
	private List<Link> links;
	
	private Page page;
	
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;

	public String execute() {
		
		return SUCCESS;
	}
	/*
	 * 获得资源列表
	 */
	public String getResourceList() {
		ResourceDAO resourceDAO = new ResourceImple();
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Resources where state = 0", page.getCurrentPage(), 30); // 获得分页对象
		resources = resourceDAO.getResourcelist(page);
		
		return SUCCESS;
	}
	
	/*
	 * 修改资源信息
	 */
	public String modifyResource() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.modifyResource(resource);
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Resources where state = 0", page.getCurrentPage(), 30); // 获得分页对象
		resources = resourceDAO.getResourcelist(page);
		return SUCCESS;
	}
	
	/*
	 * 修改我的资源信息
	 */
	public String modifyMineResource() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.modifyResource(resource);
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		resources = resourceDAO.getResourcelist(resource.getUploader().getId());
		links = resourceDAO.getLinkList(resource.getUploader().getId());
		return SUCCESS;
	}
	
	/*
	 * 删除资源
	 */
	public String deleteResource() {
		ResourceDAO resourceDAO = new ResourceImple();
		//int result = 0;
		int result = resourceDAO.deleteResource(resource.getId());
		String realP = ServletActionContext.getRequest().getRealPath("");
		String filePath = realP.substring(0,realP.length() - 6) + resource.getDocPath();
		System.out.println(filePath);
		if(result == 0) {  // 失败
			
		}else {  //成功
			result = resourceDAO.deleteResourceFile(filePath);
		}
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Resources where state = 0", page.getCurrentPage(), 30); // 获得分页对象
		resources = resourceDAO.getResourcelist(page);
		return SUCCESS;
	}
	
	/*
	 * 删除我的资源
	 */
	public String deleteMineResource() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.deleteResource(resource.getId());
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		resources = resourceDAO.getResourcelist(resource.getUploader().getId());
		links = resourceDAO.getLinkList(resource.getUploader().getId());
		return SUCCESS;
	}
	/*
	 *查看资源详细信息
	 */
	public String getResourceInfo() {
		ResourceDAO resourceDAO = new ResourceImple();
		resource = resourceDAO.getResourceById(resource.getId());
		
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_ResourcesMessage where resourceId = " + resource.getId(), page.getCurrentPage(), 20); // 获得分页对象
		resourceMessages = resourceDAO.getResourceMessage(resource.getId(),page);
		return SUCCESS;
	}
	
	/*
	 * 我的资源列表
	 */
	public String getResourceMine() {
		ResourceDAO resourceDAO = new ResourceImple();
		resources = resourceDAO.getResourcelist(user.getId());
		links = resourceDAO.getLinkList(user.getId());
		return SUCCESS;
	}
	
	/*
	 * 上传资源
	 */
	public String uploadResource() {
		ResourceDAO resourceDAO = new ResourceImple();
		
		ResourceFile resourceFile = new ResourceFile();
		resourceFile.setUpload(getUpload());
		resourceFile.setUploadContentType(getUploadContentType());
		resourceFile.setUploadFileName(getUploadFileName());
		resourceFile.setSavePath(getSavePath());
		resourceFile.setAbleSize(1024*1024);
		boolean result = resourceDAO.uploadFile(resourceFile);
		if(result) {  //上传成功
			resource.setDocPath("userfile\\res\\" + resourceFile.getUploadFileName());
			String docType = getUploadFileName().substring((getUploadFileName().lastIndexOf(".") + 1));
			resource.setDocType(docType);
			resource.setDocSize(resourceFile.getSize());
			resource.setDownloadType(getUploadContentType());
			
			int urs = resourceDAO.addFileInfo(resource);
			if(urs == 0) {  //失败
				
			}else {   //成功
			
			}
		}else {  //上传失败
			
			if (page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_Resources where state = 0", page.getCurrentPage(), 30); // 获得分页对象
			resources = resourceDAO.getResourcelist(page);
			return ERROR;
		}
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Resources where state = 0", page.getCurrentPage(), 30); // 获得分页对象
		resources = resourceDAO.getResourcelist(page);
		return SUCCESS;
	}
	
	/*
	 * 增加资源留言
	 */
	public String addRMessage() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.addRMessage(resourceMessage);
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		resource = resourceDAO.getResourceById(resourceMessage.getResource().getId());
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_ResourcesMessage where resourceId = " + resourceMessage.getResource().getId(), page.getCurrentPage(), 20); // 获得分页对象
		resourceMessages = resourceDAO.getResourceMessage(resource.getId(),page);
		return SUCCESS;
	}
	
	/*
	 * 删除资源留言
	 */
	public String deleteRMessage() {
		ResourceDAO resourceDAO = new ResourceImple();
		int result = resourceDAO.deleteRMessage(resourceMessage.getId());
		if(result == 0) {  // 失败
			
		}else {  //成功
			
		}
		resource = resourceDAO.getResourceById(resourceMessage.getResource().getId());
		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_ResourcesMessage where resourceId = " + resourceMessage.getResource().getId(), page.getCurrentPage(), 20); // 获得分页对象
		resourceMessages = resourceDAO.getResourceMessage(resource.getId(),page);
		return SUCCESS;
	}
	
	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Resource getResource() {
		return resource;
	}
	public void setResource(Resource resource) {
		this.resource = resource;
	}
	public ResourceFile getResourceFile() {
		return resourceFile;
	}
	public void setResourceFile(ResourceFile resourceFile) {
		this.resourceFile = resourceFile;
	}
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getSavePath() {
		String realPath = ServletActionContext.getRequest().getRealPath(
				savePath);
		return realPath;
	}
	
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public List<Resource> getResources() {
		return resources;
	}
	public void setResources(List<Resource> resources) {
		resources = resources;
	}
	public ResourceMessage getResourceMessage() {
		return resourceMessage;
	}
	public void setResourceMessage(ResourceMessage resourceMessage) {
		this.resourceMessage = resourceMessage;
	}
	public List<ResourceMessage> getResourceMessages() {
		return resourceMessages;
	}
	public void setResourceMessages(List<ResourceMessage> resourceMessages) {
		this.resourceMessages = resourceMessages;
	}
	public List<Link> getLinks() {
		return links;
	}
	public void setLinks(List<Link> links) {
		this.links = links;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
}
